package light.head.controller.mobile;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.constant.Url;
import light.head.dao.oa.AnnouncementDAO;
import light.head.dao.sys.MessageDAO;
import light.head.dao.sys.MessageTemplateDAO;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.Announcement;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/*
 * 公告审批手机版
 * 
 */
@Controller
@RequestMapping(Url.M_ANNMANAGER)
public class MobileAnnManagerController extends SupporterController{
	private AnnouncementDAO annDAO;
	private OrganDAO organDAO;
	private MessageDAO msgDAO;
	private MessageTemplateDAO mtDAO;

	@RequestMapping(value = Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		System.out.println("公告审批手机版");
		return new ModelAndView(Url.M_ANNMANAGER_INIT);
	}

	
	@RequestMapping(value = Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.RT, Parm.OC, Parm.START, Parm.END, Parm.TITLE,Parm.CUNAME);
		Integer oc  = organDAO.getRoot(cu_organCode(request)).getCode();
		if (annDAO.findByMap(organDAO.getSonCodeList(oc),map, pb ) == RESULT.SUCCESS) {
			request.setAttribute(Parm.PAGE_BEAN, pb);
		}
		return new ModelAndView(Url.M_ANNMANAGER_LIST);
	}

	@RequestMapping(value = Url.DETAIL)
	public ModelAndView detail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (isNotNull(code)) {
			Announcement ann = annDAO.get(Integer.parseInt(code));
			request.setAttribute(Parm.ANN, ann);
		}
		return new ModelAndView(Url.M_ANNMANAGER_DETAIL);
	}
	
	@RequestMapping(Url.UPDATEDESIGNATE_1)
	public void updateDesignate1(String code, HttpServletRequest request, HttpServletResponse response) throws Exception {
		Announcement ann = annDAO.get(Integer.parseInt(code));
		ann.setUc(cu_code(request));
		ann.setStatus(Sys.B_TWO);
		ann.setTime(new Date());
		ann.setRt(new Date());
		if (RESULT.SUCCESS == annDAO.upd(ann)) {
			String content = mtDAO.throughToCu(ann.getCt(), ann.getTime());
			msgDAO.sendToUsers("【" + cu_realName(request) + "】审核通知", content, cu_code(request), cu_organCode(request), ann.getCu());
			print(response,Msg.SUCCESS);
		}
	}
	
	@RequestMapping(Url.UPDATEDESIGNATE_2)
	public void updateDesignate2(String code, String remark,HttpServletRequest request, HttpServletResponse response) throws Exception {
		Announcement ann = annDAO.get(Integer.parseInt(code));
		ann.setUc(cu_code(request));
		ann.setStatus(Sys.B_THREE);
		ann.setTime(new Date());
		ann.setRt(new Date());
		ann.setRemark(remark);
		if (RESULT.SUCCESS == annDAO.upd(ann)) {
			String content = mtDAO.rejectTofirst(ann, cu_realName(request),ann.getCt(), remark);
			msgDAO.sendToUsers("【" + cu_realName(request) + "】驳回了申请单", content, cu_code(request), cu_organCode(request), ann.getCu());
			print(response,Msg.SUCCESS);
		}
	}

	@Autowired
	public void setAnnDAO(AnnouncementDAO annDAO) {
		this.annDAO = annDAO;
	}


	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}


	@Autowired
	public void setMsgDAO(MessageDAO msgDAO) {
		this.msgDAO = msgDAO;
	}

	@Autowired
	public void setMtDAO(MessageTemplateDAO mtDAO) {
		this.mtDAO = mtDAO;
	}
	
	
}
